import {
	Local
} from '../Local/Local.js'

import {
	Utils
} from '../Utils/Utils.js'

import {
	Camera
} from '../Camera/Camera.js'

import {
	Coord
} from '../Coord/Coord.js'

import {
	Event
} from '../Event/Event.js'
import {
	TileLayer
} from '../Layer/TileLayer.js'

import {
	ModelLayer
} from '../Layer/ModelLayer.js'
import {
	TerrainLayer
} from '../Layer/TerrainLayer.js'

/**
 * 繪圖
 */
import {
	GraphicsLayer
} from "../Layer/GraphicsLayer.js"
//编辑助手
import {
	EditHelper
} from '../Helper/EditHelper.js'

import {
	Util
} from "../Utils/Util.js"

class App extends Cesium.Viewer {

	constructor(container, option) {
		super(container, {
			baseLayerPicker: false,
			fullscreenButton: false,
			geocoder: false,
			homeButton: false,
			infoBox: false,
			sceneModePicker: false,
			selectionIndicator: false,
			timeline: false,
			navigationHelpButton: false,
			navigationInstructionsInitiallyVisible: false,
			scene3DOnly: true,
			globe: false,
			shouldAnimate: false,
			animation: false,
			contextOptions: {
				webgl: {
					alpha: true,
					depth: true,
					stencil: true,
					antialias: true,
					premultipliedAlpha: true,
					preserveDrawingBuffer: true,
					failIfMajorPerformanceCaveat: true
				}
			}
		});

		this.scene.sun.show = false; //太阳
		this.scene.moon.show = false; //月亮
		this.scene.skyBox.show = false; //天空盒
		this.scene.skyAtmosphere.show = false; //大气层
		this.scene.fog.enabled = false; //雾
		this.cesiumWidget.creditContainer.style.display = "none";

		//关闭抗锯齿
		this.scene.fxaa = false
		this.scene.postProcessStages.fxaa.enabled = false;

		//自动调整分辨率
		if (this.cesiumWidget._supportsImageRenderingPixelated) {
			var vtxf_dpr = window.devicePixelRatio;
			while (vtxf_dpr >= 2.0) {
				vtxf_dpr /= 2.0;
			}
			this.resolutionScale = vtxf_dpr;
		}

		this.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType
			.LEFT_DOUBLE_CLICK);
		this.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);

		let options = Cesium.defaultValue(option, {});
		this._url = options.url
		this._skyBox = options.skyBox
		this._background = options.background
		this._complete = options.complete
		if (this._complete) {
			this._complete();
		}
	}
	set background(background) {
		this._background = background;
	}
	get background() {
		return this._background
	}
	/**
	 * @description 相机
	 */
	get cam() {
		if (!this._cam) {
			this._cam = new Camera(this);
		}
		return this._cam;
	}

	/**
	 * @description 坐标转换
	 */
	get coord() {
		if (!this._coord) {
			this._coord = new Coord(this);
		}
		return this._coord;
	}

	/**
	 * @description 编辑助手
	 */
	get editHelper() {
		if (!this._editHelper) {
			this._editHelper = new EditHelper(this);
		}
		return this._editHelper;
	}

	/**
	 * @description 事件
	 */
	get event() {
		if (!this._event) {
			this._event = new Event(this);

		}
		return this._event;
	}

	/**
	 * @description 工具类
	 */
	get util() {
		if (!this._util) {
			this._util = new Util(this);;
		}
		return this._util;
	}

	/**
	 * @description 瓦片图层
	 */
	get tileLayer() {
		if (!this._tileLayer) {
			this._tileLayer = new TileLayer(this);
		}
		return this._tileLayer;
	}

	/**
	 * @description 模型图层
	 */
	get modelLayer() {
		if (!this._modelLayer) {
			this._modelLayer = new ModelLayer(this);
		}
		return this._modelLayer;
	}

	get graphicsLayer() {
		if (!this._graphicsLayer) {
			this._graphicsLayer = new GraphicsLayer(this);
		}
		return this._graphicsLayer;
	}

	get terrainLayer() {
		if (!this._terrainLayer) {
			this._terrainLayer = new TerrainLayer(this);
		}
		return this._terrainLayer;
	}

	get tileLayer() {
		if (!this._tileLayer) {
			this._tileLayer = new TileLayer(this);
		}
		return this._tileLayer;
	}


	/**
	 * @description 创建物体
	 * @param {Object} param{
		type:类型,
	 }
	 */
	create(option) {
		let options = Cesium.defaultValue(option, {});
		if ('Map' == options.type) {
			this.showGlobe();
			if (Cesium.defined(options.style)) {
				this.scene.globe.enableLighting = Cesium.defaultValue(options.style.night, false);
				this.shadows = Cesium.defaultValue(options.style.night, false);
				this.scene.fog.enabled = Cesium.defaultValue(options.style.fog, false);
				this.scene.fog.density = Cesium.defaultValue(options.style.density, 0)
				this.scene.fog.minimumBrightness = Cesium.defaultValue(options.style.minimumBrightness, 0.1)
				if (Cesium.defaultValue(options.style.night, false)) {
					this.scene.sun.show = true; //太阳
					this.scene.moon.show = true; //月亮
				} else {
					this.scene.sun.show = false; //太阳
					this.scene.moon.show = false; //月亮
				}
			}
			if (Cesium.defined(options.terrain)) {
				this.terrainProvider = this.terrainLayer.create(options.terrain);
			}
			if (Cesium.defined(options.atmosphere)) {
				this.scene.skyAtmosphere.show = Cesium.defaultValue(options.atmosphere, false); //大气层
			}
			if (Cesium.defined(options.atmosphere)) {
				this.scene.skyAtmosphere.show = Cesium.defaultValue(options.atmosphere, false); //大气层
			}
			if (Cesium.defined(options.atmosphere)) {
				this.scene.skyAtmosphere.show = Cesium.defaultValue(options.atmosphere, false); //大气层
			}
			if (Cesium.defined(options.complete)) {
				options.complete(this);
			}
		}
		//TileLayer
		else if ('TileLayer' == options.type) {
			return this.tileLayer.add(options.layer, options.complete);
		}
		//GraphicsLayer
		else if ('GraphicsLayer' == options.type) {
			return this.graphicsLayer.add(options.layer, options.complete);
		}
	}

	/**
	 * 显示球体
	 */
	showGlobe() {
		if (!this.scene.globe) {
			this.scene.globe = new Cesium.Globe(Cesium.Ellipsoid.WGS84);
		}
	}

	/**
	 * 隐藏球体
	 */
	hideGlobe() {
		if (this.scene.globe) {
			this.scene.globe.show = false;;
		}
	}

	/**
	 * @description 添加layer,可以添加FeatureLayer、ThingLayer、TileLayer等
	 * @param {Object} options
	 */
	addLayer(options) {
		//TileLayer
		if ('TileLayer' == options.type) {
			return this.tileLayer.add(options.layer, options.complete);
		}
		//GraphicsLayer
		else if ('GraphicsLayer' == options.type) {
			return this.graphicsLayer.add(options.layer, options.complete);
		}
		else if ('TerrainLayer' == options.type) {
			return this.terrainLayer.add(options.layer, options.complete);
		}
	}
	
	/**
	 * @description 移除
	 * @param {Object} options{
		type:类型,
		layer:{
			groupId:组,
			id:对象,
		}
	 }
	 */
	removeLayer(id){
		//TileLayer
		if ('TileLayer' == options.type) {
			return this.tileLayer.remove(options.layer.id);
		}
		//GraphicsLayer
		else if ('GraphicsLayer' == options.type) {
			return this.graphicsLayer.remove(options.layer);
		}
		//TerrainLayer
		else if ('TerrainLayer' == options.type) {
			return this.terrainLayer.remove();
		}
	}

	/**
	 * @description  飛行
	 * @param {Object} options
	 */
	flyTo(options) {
		this.cam.fly(options);
	}

	/**
	 * @description 使 3D 窗口获得焦点
	 * @param {Object} param
	 */
	focus(param) {}

	/**
	 * @description 物体查询
	 * @param {Object} param
	 */
	query(param) {

	}

	/**
	 * @description 事件绑定
	 * @param {Object} options
	 * @param {Object} callback
	 */
	on(options, callback) {
		this._event.on(options, callback);
	}

	/**
	 * @description 移除事件绑定
	 * @param {Object} options
	 * @param {Object} callback
	 */
	off(options, callback) {
		this._event.off(options, callback);
	}

	/**
	 * @description 销毁
	 */
	destroy() {
		if (this._cam) {
			this._cam.destroy();
		}
		if (this._coord) {
			this._coord.destroy();
		}
		if (this._event) {
			this._event.destroy();
		}
		if (this._tileLayer) {
			this._tileLayer.destroy();
		}
		if (this._modelLayer) {
			this._modelLayer.destroy();
		}
		if (this._editHelper) {
			this._editHelper.destroy();
		}
		if (this._graphicsLayer) {
			this._graphicsLayer.destroy();
		}
		delete this._modelLayer
		delete this._tileLayer
		delete this._coord
		delete this._event
		delete this._graphicsLayer
		delete this._editHelper
		delete this._viewer
		return Cesium.destroyObject(this);
	}
}
export {
	App
};
